package com.example.liunanxin.lnx226ordering.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.liunanxin.lnx226ordering.R;
import com.example.liunanxin.lnx226ordering.beans.CollectBean;
import com.example.liunanxin.lnx226ordering.url.Url;
import com.squareup.picasso.Picasso;

import java.util.List;

public class CollectionFoodAdapter extends RecyclerView.Adapter<CollectionFoodAdapter.ItemViewHolder> {

    private List<CollectBean> beans;
    OnItemClickListener onItemClickListener;


    public interface OnItemClickListener {
        void setItem(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener=onItemClickListener;
    }


    //刷新列表数据
    public void setData(List<CollectBean> collectBean){
        this.beans=collectBean;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view=LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_food,viewGroup,false);
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder itemViewHolder, int i) {
        CollectBean collectBean=beans.get(i);
        final int position=i;
        Picasso.get()
                .load(Url.USERURL+collectBean.getPic())
                .into(itemViewHolder.img_food);
        itemViewHolder.tv_foodname.setText(collectBean.getFoodname());
        itemViewHolder.tv_foodprice.setText("价格："+collectBean.getPrice());
        itemViewHolder.tv_foodintro.setText("");
        if(onItemClickListener!=null){
            itemViewHolder.itemview.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onItemClickListener.setItem(view,position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return beans==null?0:beans.size();
    }


    public class ItemViewHolder extends RecyclerView.ViewHolder {
        ImageView img_food;
        TextView tv_foodname,tv_foodprice,tv_foodintro;
        View itemview;

        public ItemViewHolder(@NonNull View itemView) {
            super(itemView);
            img_food=itemView.findViewById(R.id.imageView7);
            tv_foodname=itemView.findViewById(R.id.textView10);
            tv_foodprice=itemView.findViewById(R.id.textView11);
            tv_foodintro=itemView.findViewById(R.id.textView12);
            itemview=itemView;
        }
    }
}
